<template>
  <div class="tenant-container">
    <!-- 用户名称搜索  状态搜索 -->
    <ContentWrap>
      <el-form class="-mb-15px" ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="会员ID" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入会员ID" class="!w-240px" />
        </el-form-item>
        <el-form-item label="会员信息" prop="mobile">
          <el-input v-model="queryParams.mobile" placeholder="请输入昵称/姓名/手机号" class="!w-240px" />
        </el-form-item>
        <el-form-item label="订单号" prop="tradeNo">
          <el-input v-model="queryParams.tradeNo" placeholder="请输入订单号" class="!w-240px" />
        </el-form-item>
        <el-form-item label="类型" prop="channel">
          <el-select v-model="queryParams.channel" clearable placeholder="请选择类型" class="!w-240px">
            <el-option
v-for="item in getDictOptions(DICT_TYPE.TONGUE_OBTAIN_TYPE)" :key="item.value"
              :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="获得时间" prop="createTime">
          <el-date-picker
v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
            start-placeholder="开始日期" end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <!-- 记录列表 -->
    <ContentWrap>
      <el-table :data="tableData" :stripe="true" :show-overflow-tooltip="true" :loading="loading">
        <el-table-column label="获得时间" align="center" prop="createTime" :formatter="dateFormatter" width="180" />
        <el-table-column label="会员Id" prop="userId" align="center" />
        <el-table-column label="会员" prop="mobile" align="center" />
        <el-table-column label="计费方式" prop="purchaseType" align="center">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.TONGUE_BILLING_METHOD" :value="scope.row.purchaseType" />
          </template>
        </el-table-column>
        <el-table-column label="订单号" prop="tradeNo" align="center" />
        <el-table-column label="类型" prop="channel" align="center">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.TONGUE_OBTAIN_TYPE" :value="scope.row.channel" />
          </template>
        </el-table-column>
        <el-table-column label="获得次数" prop="times" align="center" />
        <el-table-column label="有效期" align="center" prop="expiredTime" :formatter="dateFormatter" width="180" />
      </el-table>
      <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    </ContentWrap>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import { TongueBillApi } from '@/api/application/tongue/bill'
import * as LevelApi from '@/api/member/level'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'

const state = reactive({
  queryParams: {
    userId: '',
    mobile: '',
    tradeNo: '',
    channel: '',
    createTime: [],
    level: null,
    pageNo: 1,
    pageSize: 10
  },
  loading: false,
  tableData: [],
  levelList: [],
  total: 0,
  addTenantVisible: false,
  editType: 'add',
  editData: {}
})
const queryFormRef = ref() // 搜索的表单
const { queryParams, loading, tableData, total, levelList } = toRefs(state)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TongueBillApi.getMemberReport(queryParams.value)
    tableData.value = data.list
    total.value = data.total
    // console.log(data)
    // console.log(tableData.value)
  } finally {
    loading.value = false
  }
}
/** 查询会员等级列表 */
const getLevel = async () => {
  levelList.value = await LevelApi.getLevelList({})
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const handleReset = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
onMounted(() => {
  getList()
  getLevel()
})
</script>

<style scoped lang="scss"></style>